﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace LinqToSQL.Manning
{
    class Linq
    {

        #region Constructor
        private static Linq _Instance = null;
        private Linq()
        {
        }
        static readonly private object padLock = new object();
        public static Linq Instance
        {
            get
            {
                if (_Instance == null)
                    lock (padLock)
                    {
                        if (_Instance == null)
                            _Instance = new Linq();
                    }
                return _Instance;
            }
        }
        #endregion

        public class Product
        {
            public string Name { get; set; }
            public decimal Price { get; set; }
            public int SupplierId { get; set; }
        }

        public enum EntryType { Error}

        public class LogEntry
        {
            public LogEntry(string line)
            {
                this.Line = line;
            }
            public string  Line { get; set; }
            public EntryType Type { get; set; }
        }

        public void XmlTest()
        {
            XDocument doc = XDocument.Load(@"./ExampleFiles/Manning.Linq.XmlTest.xml");
            var filtered = from p in doc.Descendants("Product")
                           join s in doc.Descendants("Supplier")
                           on (int)p.Attribute("SupplierID")
                           equals (int)s.Attribute("SupplierID")
                           where (decimal)p.Attribute("Price") > 10
                           orderby (string)s.Attribute("Name"),
                           (string)p.Attribute("Name")
                           select new
                           {
                               SupplierName = (string)s.Attribute("Name"),
                               ProductName = (string)p.Attribute("Name")
                           };
            foreach (var v in filtered)
            {
                Console.WriteLine("Supplier={0}; Product={1}",
                v.SupplierName, v.ProductName);
            }           
        }

        public void ProyectionSpecialCase()
        {
            string logDirectory = "";            
            var query = from file in Directory.GetFiles(logDirectory, "*.log")
                        from line in File.ReadAllLines(file)
                        let entry = new LogEntry(line)
                        where entry.Type == EntryType.Error
                        select entry;
        }

        public void AggregateExample()
        {
            var data = Enumerable.Range(-10, 21).ToList<int>();

            var result = data.Aggregate<int, List<int>, List<string>>(new List<int>(),
                (list, item) => { if (item % 2 == 0) list.Add(item); return list; },
                list => list.ConvertAll(item => item.ToString()));
            string final = result.Aggregate((acum, item) => acum += string.Format(", {0}", item));
        }
    }
}
